package com.example.tests;

import com.thoughtworks.xstream.XStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static com.example.fw.RandomHelper.getNullable;
import static com.example.fw.RandomHelper.getRandomStringValue;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 26.03.13
 * Time: 16:47
 * To change this template use File | Settings | File Templates.
 */
public class GroupDataGenerator {
    private static void saveGroupsToCSV(List<GroupData> groupDataList, File outputFile) throws IOException {
        FileWriter fileWriter = new FileWriter(outputFile);
        for (GroupData groupData : groupDataList){
            fileWriter.write(
                    groupData.getGroupName() + ","
                    + groupData.getGroupHeader() + ","
                    + groupData.getGroupFooter() + '\n'
            );
        }
        fileWriter.close();
    }

    private static void saveGroupsToXML(List<GroupData> groupDataList, File outputFile) throws IOException {
        XStream xStream = new XStream();
        xStream.alias("group", GroupData.class);
        String xml = xStream.toXML(groupDataList);
        FileWriter fileWriter = new FileWriter(outputFile);
        fileWriter.write(xml);
        fileWriter.close();
    }

    public static void main(String[] args) throws IOException {

        if (args.length < 3){
            System.out.println("Please specify parameters: <amount of test data> <file> <format>");
            return;
        }

        int amount = Integer.parseInt(args[0]);
        File outputFile = new File(args[1]);
        String format = args[2];

        if (outputFile.exists()){
            System.out.println(outputFile + " already exists");
            return;
        }

        List<GroupData> groupDataList = generateRandomGroups(amount);

        if ("csv".equals(format)){
            saveGroupsToCSV(groupDataList, outputFile);
        }
        else if ("xml".equals(format)){
            saveGroupsToXML(groupDataList, outputFile);
        }
        else{
            System.out.println("unsupported file format " + format);
        }
        return;

    }

    public static List<GroupData> loadGroupsFromCSV(File inputFile) throws IOException {
        List<GroupData> resultList = new ArrayList<GroupData>();
        FileReader fileReader = new FileReader(inputFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String nextLine = bufferedReader.readLine();
        while (nextLine!= null) {
            String[] splitLine = (nextLine + ",-").split(",");
            resultList.add(
                    new GroupData()
                            .setName(splitLine[0])
                            .setHeader(splitLine[1])
                            .setFooter(splitLine[2])
            );
            nextLine = bufferedReader.readLine();
        }
        bufferedReader.close();
        return resultList;
    }

    public static List<GroupData> loadGroupsFromXML (File inputFile) throws IOException {
        XStream xStream = new XStream();
        xStream.alias("group", GroupData.class);
        return (List<GroupData>)xStream.fromXML(inputFile);
    }


    public static List<GroupData> generateRandomGroups(int groupsCount) {
        List<GroupData> result = new ArrayList<GroupData>();
        int randomStringMaxLength = 15;
        int nullFreq = 3;
        for(int i = 0; i < groupsCount; i++){
            GroupData groupData = new GroupData()
                    .setName(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .setHeader(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq))
                    .setFooter(getNullable(getRandomStringValue(randomStringMaxLength), nullFreq));
            result.add(groupData);
        }
        return result;
    }
}
